<template>
  <div class="jsbos-common-layout">
    <div class="jsbos-common-layout-center">
      <el-row class="jsbos-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="4">
            <el-form-item label="关键词">
              <el-input
                v-model="listQuery.keyword"
                placeholder="请输入"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="类型" prop="webhookType">
              <el-select
                v-model="query.webhookType"
                placeholder="请选择"
                clearable
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="(item, index) in webHookTypeOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.enCode"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="状态" prop="enabledMark">
              <el-select
                v-model="query.enabledMark"
                placeholder="请选择"
                clearable
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="(item, index) in enabledMarkOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.id"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()"
                >查询</el-button
              >
              <el-button icon="el-icon-refresh-right" @click="reset()"
                >重置</el-button
              >
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="jsbos-common-layout-main jsbos-flex-main">
        <div class="jsbos-common-head">
          <div>
            <!-- v-has="'btn_add'" -->
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="addOrUpdateHandle()"
              >新增
            </el-button>
          </div>
          <div class="jsbos-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link
                icon="icon-ym icon-ym-Refresh JSBOS-common-head-icon"
                :underline="false"
                @click="reset()"
              />
            </el-tooltip>
          </div>
        </div>
        <jsbos-table
          v-loading="listLoading"
          :data="list"
          :border="true"
          style="margin-left: 5px"
          :isHeader="false"
          @sort-change="sortChange"
        >
          <el-table-column
            prop="fullName"
            label="名称"
            min-width="150"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="enCode"
            label="编码"
            min-width="150"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="webhookType"
            label="WebHook类型"
            width="150"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="address"
            label="应用地址"
            min-width="150"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="sortCode"
            label="排序"
            width="50"
            :align="'center'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="enabledMark"
            label="状态"
            width="70"
            :align="'center'"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.enabledMark == 1 ? 'success' : 'danger'"
                disable-transitions
              >
                {{ scope.row.enabledMark == 1 ? "启用" : "禁用" }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column
            prop="creatorTime"
            label="创建时间"
            width="180"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="creatorUserId"
            label="创建用户"
            width="100"
            :align="'center'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="lastModifyTime"
            label="修改时间"
            width="180"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="lastModifyUserId"
            label="修改用户"
            width="100"
            :align="'center'"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="操作" fixed="right" width="150">
            <template slot-scope="scope">
              <!-- v-has="'btn_edit'" -->
              <el-button type="text" @click="addOrUpdateHandle(scope.row.id)"
                >编辑
              </el-button>
              <!-- v-has="'btn_remove'" -->
              <el-button
                type="text"
                class="jsbos-table-delBtn"
                @click="handleDel(scope.row.id)"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </jsbos-table>
        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="initData"
        />
      </div>
    </div>
    <JSBOS-Form
      v-if="formVisible"
      ref="JSBOSForm"
      :category="category"
      @refresh="refresh"
    />
  </div>
</template>

<script>
import { getList, Delete } from "@/views/message/api/mcAccountConfig";
import JSBOSForm from "./Form";

export default {
  name: "webhookConfig",
  components: { JSBOSForm },
  data() {
    return {
      category: "8", // webhook
      detailVisible: false,
      query: {
        fullName: undefined,
        enCode: undefined,
        webhookType: undefined,
      },
      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        keyword: "",
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "creatorTime",
      },
      formVisible: false,
      webHookTypeOptions: [],
      enabledMarkOptions: [
        { fullName: "启用", id: "1" },
        { fullName: "禁用", id: "0" },
      ],
    };
  },
  computed: {
    menuId() {
      return this.$route.meta.modelId || "";
    },
  },
  created() {
    this.initData();
    this.getWebHookTypeOptions();
  },
  methods: {
    // WebHook类型 匹配 数据字典数据
    matchingWebhookType(e) {
      for (var i = 0; i < this.webHookTypeOptions.length; i++) {
        if (this.webHookTypeOptions[i].enCode == e) {
          return this.webHookTypeOptions[i].fullName;
        }
      }
    },
    getWebHookTypeOptions() {
      this.$store
        .dispatch("base/getDictionaryData", {
          encode: "msgWebHookSendType",
        })
        .then((res) => {
          this.webHookTypeOptions = res;
        });
    },
    sortChange({ column, prop, order }) {
      this.listQuery.sort = order == "ascending" ? "asc" : "desc";
      this.listQuery.sidx = !order ? "" : prop;
      this.initData();
    },
    initData() {
      this.listLoading = true;
      let _query = {
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
        category: this.category,
      };
      getList(_query).then((res) => {
        var _list = [];
        for (let i = 0; i < res.data.list.length; i++) {
          let _data = res.data.list[i];
          _list.push(_data);
        }
        this.list = _list;
        this.total = res.data.pagination.total;

        this.listLoading = false;
      });
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          Delete(id).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.JSBOSForm.init(id, isDetail);
      });
    },
    search() {
      this.initData();
    },
    refresh(isInitData) {
      this.formVisible = false;
      if (isInitData) this.initData();
    },
    resetListQuery() {
      this.listQuery = {
        keyword: "",
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "creatorTime",
      };
    },
    resetQuery() {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
    },
    reset() {
      this.resetListQuery();
      this.resetQuery();
      this.initData();
    },
  },
};
</script>
